<?php
define ( 'LIB', dirname ( __FILE__ ) );

/**
 * 自动加载对象文件
 *
 * @param string $sClassName 对象名
 */
function __autoload($sClassName) {
	$sClassName = str_replace ( '_', DIRECTORY_SEPARATOR, $sClassName );
	$sClassName = strtolower ( $sClassName );
	if (is_file ( LIB . '/' . $sClassName . '.class.php' )) {
		require_once LIB . '/' . $sClassName . '.class.php';
	}
}

class Base {
	/**
	 * 配置文件参数
	 *
	 * @var array
	 */
	
	public static $aConf;
	/**
	 * 所有加载的对象
	 *
	 * @var array
	 */
	public static $aClass = array ();
	
	public static $aClassPath = array ();
	
	/**
	 * 初始化方法
	 *
	 * @param string $sConfigFile 配置文件名
	 */
	public static function init($sConfigFile = null) {
		Base::parseConfigFile ( $sConfigFile, true );
	}
	
	/**
	 * 获取配置文件中的值
	 *
	 * @param string $sName
	 * @return string
	 */
	public static function getConf($sName, $sConfigFile = null) {
		if ($sConfigFile) {
			$sConfId = md5 ( realpath ( $sConfigFile ) );
		} else {
			$sConfId = 'default';
		}
		
		list ( $sType, $sName ) = explode ( '.', $sName );
		if ($sName) {
			$sValue = Base::$aConf [$sConfId] [$sType] [$sName];
			if ($sValue == null) {
				$aParentValue = Base::$aConf [$sConfId] [$sType];
				if (is_array ( $aParentValue )) {
					$sValue = Base::parseConf ( $aParentValue, $sName );
				}
			}
			return $sValue;
		} else {
			return Base::$aConf [$sConfId] [$sType];
		}
	}
	
	public function setConf($sName, $sValue, $sConfigFile = null) {
		if ($sConfigFile) {
			$sConfId = md5 ( realpath ( $sConfigFile ) );
		} else {
			$sConfId = 'default';
		}
		
		list ( $sType, $sName ) = explode ( '.', $sName );
		Base::$aConf [$sConfId] [$sType] [$sName] = $sValue;
	}
	
	public static function parseConfigFile($sConfigFile = null, $bDefault = false) {
		if ($bDefault) {
			$sConfId = 'default';
		} else {
			$sConfId = md5 ( realpath ( $sConfigFile ) );
		}
		Base::$aConf [$sConfId] = parse_ini_file ( $sConfigFile, true );
	}
	
	/**
	 * 实例化对象
	 * 如果该对象已经实例化，则返回已经实例化后的对象
	 *
	 * @param string $sClassName
	 * @return object
	 */
	public static function loadClass($sClassName, $sType = 'lib') {
		if ($sType == 'lib') {
			$sPath = LIB;
		} else {
			$sPath = Base::getClassPath ( $sType );
		}
		try {
			if (strpos ( $sClassName, '.' ) !== false) {
				$sClassName = substr ( $sClassName, strrpos ( '.', $sClassName ) + 1 );
				$sClassFile = $sPath . '/' . strtolower ( str_replace ( '.', '/', $sClassName ) ) . '.class.php';
			} else {
				$sClassFile = $sPath . '/' . strtolower ( $sClassName ) . '.class.php';
			}
			if (! is_file ( $sClassFile )) {
				throw new ExceptionExt ( '类文件' . $sClassFile . '不存在' );
			}
			require_once $sClassFile;
			$sClassCode = $sType . '_' . $sClassName;
			
			if (! array_key_exists ( $sClassCode, Base::$aClass )) {
				Base::$aClass [$sClassCode] = new $sClassName ( );
			}
		} catch ( ExceptionExt $e ) {
			$e->showError ( true );
		}
		
		return Base::$aClass [$sClassCode];
	}
	
	public static function getClassPath($sType, $sPath = null) {
		if ($sPath == null && Base::$aClassPath [$sType]) {
			return Base::$aClassPath [$sType];
		}
		$sPath = $sPath ? $sPath : Base::getConf ( 'base.' . $sType . '_path' );
		if ($sPath == null) {
			$sRealPath = realpath ( LIB . '/../' . $sType );
		} else {
			$sRealPath = realpath ( $sPath );
		}
		if (! $sRealPath) {
			$sRealPath = realpath ( LIB . '/' . $sPath );
		}
		
		try {
			if (! $sRealPath) {
				throw new ExceptionExt ( '类文件' . $sPath . '不存在' );
			}
		} catch ( ExceptionExt $e ) {
			$e->showError ( true );
		}
		
		Base::$aClassPath [$sType] = $sRealPath;
		return $sRealPath;
	}
	
	public static function parseConf($aValue, $sName) {
		$aResult = null;
		foreach ( $aValue as $sKey => $sRow ) {
			$aMatch = array ();
			preg_match ( '/^' . $sName . '_{0,1}(\d*)$/i', $sKey, $aMatch );
			if (count ( $aMatch )) {
				$aResult [$aMatch [1]] = $sRow;
			}
		}
		return $aResult;
	}
}
?>